import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class LoginPanel extends JPanel{
	//JFrame frame=new JFrame("User Login");
	JLabel username=new JLabel("Username: ");
	JLabel password=new JLabel("Password: ");
	JTextField usertext=new JTextField(10);
	//JTextField passwordtext=new JTextField(10);
	JPasswordField passwordtext=new JPasswordField(10);
	JButton loginButton=new JButton();
	JPanel panel=new JPanel();
	char[] pin=passwordtext.getPassword();//hide password
	Image img;
	static String uid,pass;
	int sid;
	
	
//	http://db4free.net
//		username = studentreg14
//		password = KCTSkcts
	
	
	public LoginPanel() {
		img=new ImageIcon("login_background.png").getImage();
		final ImageIcon log=new ImageIcon("login_button.png");
		final ImageIcon clog=new ImageIcon("login_cbutton.png");
		loginButton.setBorderPainted(false);
		loginButton.setBorder(null);
		//button.setFocusable(false);
		loginButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				uid=usertext.getText();
			   	 pass=passwordtext.getText();
			   	 
             	 
             	if(loginCheck(uid,pass)){
          		 if(sid!=-1){//if not administrator, access student menu
       			remove(panel);
       			add(new MenuPanel());
       			validate();
       			repaint();}
          		 else{//if admin, sid=-1, access admin menu
          			 remove(panel);
            			add(new AdminMenuPanel());
            			validate();
            			repaint();
          		 }
             	}
			}
             	
		});
		loginButton.setMargin(new Insets(0, 0, 0, 0));
		loginButton.setContentAreaFilled(false);
		loginButton.setIcon(log);
		loginButton.setRolloverIcon(clog);
		loginButton.setPressedIcon(clog);
		loginButton.getModel().addChangeListener(new ChangeListener() {
	        @Override
	        public void stateChanged(ChangeEvent e) {
	            ButtonModel model = (ButtonModel) e.getSource();
	            if (model.isRollover()) {
	            	loginButton.setIcon(clog);
	            } else {
	            	loginButton.setIcon(log);
	            }// etc ...//  if (model.isPressed()) {
	         }
	    });
		
		//b.setRolloverIcon(myIcon2);
		//b.setPressedIcon(myIcon3);
		
		
		//JPanel backPanel=new JPanel();
		//backPanel.setPreferredSize(new Dimension(650,500));
		//backPanel.setLayout(new GridBagLayout());
		//backPanel.add(panel);
		//backPanel.setOpaque(false);
		
		panel.setPreferredSize(new Dimension(600,400));
		setBackground(Color.white);
		//panel.setBackground(Color.white);
		panel.setOpaque(false);
		panel.setLayout(new GridBagLayout());
		JLabel l=new JLabel("");
		
		JPanel p=new JPanel();
		p.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		//c.anchor = GridBagConstraints.PAGE_END;
		c.fill = GridBagConstraints.HORIZONTAL;
		p.add(username,c);
		p.add(usertext,c);
		p.add(password,c);
		p.add(passwordtext,c);
		p.add(loginButton,c);
		//p.setBackground(Color.white);
		p.setOpaque(false);
		
		setOpaque(false);
		
		panel.add(p);
		
		
		setLayout(new BorderLayout());
		add(panel,BorderLayout.PAGE_START);
		//add(backPanel,BorderLayout.PAGE_START);
		
		
		
		//find another listener that doesn't need enter key.( maybe key listener)
		usertext.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	uid=usertext.getText();
              	 pass=passwordtext.getText();
              	 
            	 if(loginCheck(uid,pass)){
            		 if(sid!=-1){//if not administrator, access student menu
         			remove(panel);
         			add(new MenuPanel());
         			validate();
         			repaint();}
            		 else{//if admin, sid=-1, access admin menu
            			 remove(panel);
              			add(new AdminMenuPanel());
              			validate();
              			repaint();
            		 }
            	}
            } });		
		passwordtext.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	uid=usertext.getText();
              	 pass=passwordtext.getText();
              	 
              	if(loginCheck(uid,pass)){
           		 if(sid!=-1){//if not administrator, access student menu
        			remove(panel);
        			add(new MenuPanel());
        			validate();
        			repaint();}
           		 else{//if admin, sid=-1, access admin menu
           			 remove(panel);
             			add(new AdminMenuPanel());
             			validate();
             			repaint();
           		 }
           	}
            } });
		
		
	}
		
	
	
	@Override
    public void paintComponent(Graphics g) {
		super.paintComponents(g);
        g.drawImage(img, 0, 0, this);
    }
	
	
	private boolean loginCheck(String u, String p){
		System.out.println(u+" "+p);
		try{
		String dName = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://db4free.net:3306/studentreg14?user=studentreg14&password=KCTSkcts";
		Connection con = DriverManager.getConnection(url);
		java.sql.Statement mysqlStatement = con.createStatement();
		java.sql.ResultSet resultSet = mysqlStatement.executeQuery("SELECT password, SID FROM student_list WHERE username='"+u+"'");
		while (resultSet.next()) {
		String pass = resultSet.getString(1);
		sid=resultSet.getInt(2);
		if (p.equals(pass)) {System.out.println("Log in succesful!"); return true;}
		else {System.out.println("Log in failed!");return false;}
		}
		con.close();
		}
		catch(Exception ex){}
		return false;
	}
}
